info = """
****************************************************************************
欢迎使用【学生信息管理系统】V1.0
请选择你想要进行的操作：
1.添加学生信息
2.删除学生信息
3.修改学生信息
4.查询学生信息
5.显示所有学生信息
0.退出系统
****************************************************************************
输入你的选择：
"""
import json

students =  [
    {'name':'张三','age':18,'math':90,'chinese':80,'english':90,'total':260},
    {'name':'李四','age':19,'math':80,'chinese':90,'english':80,'total':250},
    {'name':'王五','age':20,'math':80,'chinese':80,'english':80,'total':240},
    {'name':'赵六','age':21,'math':90,'chinese':90,'english':90,'total':270},
    {'name':'孙七','age':22,'math':80,'chinese':80,'english':80,'total':240},
    {'name':'周八','age':23,'math':80,'chinese':80,'english':80,'total':240},
    {'name':'吴九','age':24,'math':80,'chinese':80,'english':80,'total':240},
    {'name':'郑十','age':25,'math':80,'chinese':80,'english':80,'total':240},
    {'name':'王十一','age':26,'math':80,'chinese':80,'english':80,'total':240},
    {'name':'张十二','age':27,'math':80,'chinese':80,'english':80,'total':240},    
]

f = open(r'C:\Users\sea_0\Desktop\silicon\students.json','w',encoding='UTF-8')#以覆盖写的方式打开
f.write(str(students))

with open('students.json', 'w', encoding='utf-8') as f:
    json.dump(students, f, ensure_ascii=False, indent=4)

while True:
    print(info)
    #从外部输入用户操作
    action = input('')
    if action == '1':
        print('1.添加学生信息')
        name = input('请输入学生姓名：')
        age = input('请输入学生年龄：')
        math = input('请输入学生数学成绩：')
        chinese = input('请输入学生语文成绩：')
        english = input('请输入学生英语成绩：')
        total = int(math) + int(chinese) + int(english)
        students.append({'name':name,'age':age,'math':math,'chinese':chinese,'english':english,'total':total})
        print('添加成功！')
    elif action == '2':
        #print('2.删除学生信息')
        name = input('请输入想要删除的学生姓名：')
        for student in students:
            if student['name'] == name:
                students.remove(student)
                #del stundes[students,index(student)]
                print('删除成功！')
                break
        else:
            print('没有该学生！')
    elif action == '3':
        #print('3.修改学生信息')
        name = input('请输入想要修改的学生姓名：')
        print('如果输入为空，则不修改该学生信息')
        for student in students:
            if student['name'] == name:
                age  = input('请重新输入学生年龄：')
                if  age:
                    age = int(age)
                    student['age'] = age
                
                math = input('请重新输入学生数学成绩：')
                if math:
                    math = int(math)
                    student['math'] = math

                chinese = input('请重新输入学生语文成绩：')
                if chinese:
                    chinese = int(chinese)
                    student['chinese'] = chinese

                english = input('请重新输入学生英语成绩：')
                if english:
                    english = int(english)
                    student['english'] = english

                student['total'] = student['math'] + student['chinese'] + student['english']
                print('修改成功！')
                break
            else:
                print('没有该学生！')

    elif action == '4':
        #print('4.查询学生信息')
        name = input('请输入学生姓名：')
        for student in students:
            if student['name'] == name:
                print('姓名\t年龄\t数学\t语文\t英语\t总分')
                print('%s\t%s\t%d\t%d\t%d\t%d' % (student['name'], student['age'], student['math'], student['chinese'], student['english'], student['total']))
                break
        else:
            print('没有该学生！')
    elif action == '5':
        print('5.显示所有学生信息')
        print('姓名\t数学\t语文\t英语\t总分')
        for student in students:
            print('%s\t%d\t%d\t%d\t%d' % (student['name'], student['math'], student['chinese'], student['english'], student['total']))
            
    elif action == '0':
        print('0.退出系统')
        with open('students.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(students, ensure_ascii=False))
        break
    else:
        print('输入错误，请重新输入！')
    